<?php
/**
 * Author:xsc
 * Time:2023/10/7 16:50
 */

namespace App\Repository;

use App\Models\SpecialPrivilegeOrder;

class SpecialPrivilegeOrderRepo extends BaseRepo
{

    /**
     * 新增/更新赋能订单
     * @param array $data
     * @param int $id
     * @return int
     * Author:xsc
     * Time:2023/9/6 15:56
     */
    public function save(array $data, int $id = 0)
    {
        if ($id) {
            return SpecialPrivilegeOrder::query()->where('id', $id)->update($data);
        } else {
            return SpecialPrivilegeOrder::query()->insertGetId($data);
        }
    }

    /**
     * 根据赋能ids,批量查询订单
     * @param array $privilege_ids
     * @param int $goods_id
     * @return array
     * Author:xsc
     * Time:2023/10/8 11:12
     */
    public function getListByPrivilegeIds(array $privilege_ids, int $goods_id)
    {
        $where = [
            ['goods_id', '=', $goods_id],
            ['status', '=', 1],
        ];
        return SpecialPrivilegeOrder::query()->where($where)->whereIn('privilege_id', $privilege_ids)->get()->toArray();
    }

    /**
     * 详情
     * @param array $params
     * @return array
     * Author:xsc
     * Time:2023/10/10 11:10
     */
    public function detail(array $params)
    {
        $where = [];
        if (isset($params['id'])) {
            $where[] = ['id', '=', $params['id']];
        }
        if (isset($params['ordersn'])) {
            $where[] = ['ordersn', '=', $params['ordersn']];
        }
        if (isset($params['goods_id'])) {
            $where[] = ['goods_id', '=', $params['goods_id']];
        }
        if (isset($params['coupon_code'])) {
            $where[] = ['coupon_code', '=', $params['coupon_code']];
        }
        if (isset($params['thirdNo'])) {
            $where[] = ['thirdNo', '=', $params['thirdNo']];
        }
        $res = SpecialPrivilegeOrder::query()
            ->where($where)
            ->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 列表
     * @param array $params
     * @return array
     * Author:xsc
     * Time:2023/10/25 14:07
     */
    public function getList(array $params)
    {
        $where = [];
        if (isset($params['privilege_id'])) {
            $where[] = ['privilege_id', '=', $params['privilege_id']];
        }
        if (isset($params['special_id'])) {
            $where[] = ['special_id', '=', $params['special_id']];
        }
        if (isset($params['goods_id'])) {
            $where[] = ['goods_id', '=', $params['goods_id']];
        }
        if (isset($params['member_id'])) {
            $where[] = ['member_id', '=', $params['member_id']];
        }
        if (isset($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }
        if (isset($params['neq_status'])) {
            $where[] = ['status', '<>', $params['neq_status']];
        }
        return SpecialPrivilegeOrder::query()->where($where)->get()->toArray();
    }

}
